Micron Document




JavaScript syntax
part 41/59 · 107.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Control structures

Compound statements

A pair of curly brackets { } and an enclosed sequence of statements constitute a compound statement, which can be used wherever a statement can be used.

If ... else

if (expr) {
//statements;
} else if (expr2) {
//statements;
} else {
//statements;
}

Conditional (ternary) operator

The conditional operator creates an expression that evaluates as one of two expressions depending on a condition. This is similar to the if statement that selects one of two statements to execute depending on a condition. I.e., the conditional operator is to expressions what if is to statements.

const result = condition ? expression : alternative;

is the same as:

if (condition) {
const result = expression;
} else {
const result = alternative;
}

Unlike the if statement, the conditional operator cannot omit its "else-branch".

Switch statement

The syntax of the JavaScript switch statement is as follows:

switch (expr) {
case SOMEVALUE:
// statements;
break;
case ANOTHERVALUE:
// statements for when ANOTHERVALUE || ORNAOTHERONE
// no break statement, falling through to the following case
case ORANOTHERONE:
// statements specific to ORANOTHERONE (i.e. !ANOTHERVALUE && ORANOTHER);
break; //The buck stops here.
case YETANOTHER:
// statements;
break;
default:
// statements;
break;
}

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────